package com.liunian.algorithmstudy.tree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class FindMode501 extends TreeNode {

	private int count = 1;

	private int maxCount = 1;

	private TreeNode preNode;

	public int[] findMode(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		travelsal(root, list);
		int i = 0;
		int[] res = new int[list.size()];
		for (Integer l : list) {
			res[i++] = l;
		}
		return res;
	}

	private void travelsal(TreeNode root, List<Integer> list) {
		if (root == null) {
			return;
		}
		travelsal(root.left, list);
		if (preNode != null && preNode.val == root.val) {
			count++;
		} else {
			count = 1;
		}
		if (count == maxCount) {
			list.add(root.val);
		} else if (count > maxCount) {
			maxCount = count;
			list.clear();
			list.add(root.val);
		}
		preNode = root;
		travelsal(root.right, list);
	}


}
